<?php

namespace app\api\model\kuyou\applet;

use app\api\controller\kuyou\applet\Commons;
use think\Model;
use think\Db;
use Exception;
class AccountsTransfer extends Model
{

    // 表名
    protected $name = 'kuyou_accounts_transfer';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    /**
     * 获取列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     */
    public static function getList($where,$page,$limit,$type,$sort_field,$sort_type){
        $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
        $pageStatus = true;
        if($app_page){
            if($page<0){
                $pageStatus = false;
            }
        }
        if($pageStatus){
            $list = collection(self::where(function ($query) use ($where) {
                $query->where($where);
            })->order($sort_field,$sort_type)->page($page,$limit)->field("id,purchase_code,FROM_UNIXTIME(purchase_time,'%Y-%m-%d') as purchase_time,
            money,preparation_id,examine_id,examine_status,print_num,remark,
            FROM_UNIXTIME(examine_time,'%Y-%m-%d') as examine_time")->select())->toArray();
        }else{
            $list = collection(self::where(function ($query) use ($where) {
                $query->where($where);
            })->order($sort_field,$sort_type)->field("id,purchase_code,FROM_UNIXTIME(purchase_time,'%Y-%m-%d') as purchase_time,
            money,preparation_id,examine_id,examine_status,print_num,remark,
            FROM_UNIXTIME(examine_time,'%Y-%m-%d') as examine_time")->select())->toArray();
        }

        $account = (new Accounts())->column('account_name','id');
        foreach ($list as $list_k=>$list_v){
            $list[$list_k]['preparation_name'] = (new User())->where(['id'=>$list_v['preparation_id']])->value('username');
            $list[$list_k]['examine_name'] = (new User())->where(['id'=>$list_v['examine_id']])->value('username');
            unset($list[$list_k]['preparation_id']);
            unset($list[$list_k]['examine_id']);

            $info_list = Db::name('kuyou_accounts_transfer_info')
                ->where(['purchase_id'=>$list_v['id']])
                ->field('accounts_out_id,accounts_enter_id,money')
                ->select();
            foreach ($info_list as $info_list_k=>$info_list_v){
                $info_list[$info_list_k]['accounts_out'] = isset($account[$info_list_v['accounts_out_id']])?$account[$info_list_v['accounts_out_id']]:'';
                $info_list[$info_list_k]['accounts_enter'] = isset($account[$info_list_v['accounts_enter_id']])?$account[$info_list_v['accounts_enter_id']]:'';
            }
            $list[$list_k]['info_list'] = $info_list;
        }
        if($type==1){
            $lists = $list;$list=[];
            foreach ($lists as $lists_k=>$lists_v){
                foreach ($lists_v['info_list'] as $k=>$v){
                    unset($lists_v['info_list']);
                    $row = $lists_v;
                    $row['accounts_out_id'] = $v['accounts_out_id'];
                    $row['accounts_enter_id'] = $v['accounts_enter_id'];
                    $row['accounts_out'] = $v['accounts_out'];
                    $row['accounts_enter'] = $v['accounts_enter'];
                    $row['accounts_money'] = $v['money'];
                    $list[] = $row;
                }
            }
        }
        $count = self::where(function ($query) use ($where) {
            $query->where($where);
        })->count();
        return ['list'=>$list,'count'=>$count];
    }

    /**
     * 获取详情
     * @param string $purchase_code
     * @return array
     */
    public static function getDetail($purchase_code){
        $detail = self::where(function ($query) use ($purchase_code) {
            $query->where('purchase_code', '=', $purchase_code);
        })->field("id,purchase_code,FROM_UNIXTIME(purchase_time,'%Y-%m-%d') as purchase_time,
            money,preparation_id,examine_id,examine_status,print_num,remark,
            FROM_UNIXTIME(examine_time,'%Y-%m-%d') as examine_time")->find();
        if($detail){
            $account = (new Accounts())->column('account_name','id');
            $settlementtype = (new Settlementtype())->column('name','id');
            $info_list = Db::name('kuyou_accounts_transfer_info')
                ->where(['purchase_id'=>$detail['id']])
                ->field('accounts_out_id,accounts_enter_id,money,settlementtype_id,settlement_number,remark')
                ->select();
            foreach ($info_list as $info_list_k=>$info_list_v){
                $info_list[$info_list_k]['settlementtype'] = isset($settlementtype[$info_list_v['settlementtype_id']])?$settlementtype[$info_list_v['settlementtype_id']]:'';
                $info_list[$info_list_k]['accounts_out'] = isset($account[$info_list_v['accounts_out_id']])?$account[$info_list_v['accounts_out_id']]:'';
                $info_list[$info_list_k]['accounts_enter'] = isset($account[$info_list_v['accounts_enter_id']])?$account[$info_list_v['accounts_enter_id']]:'';
            }
            $detail['info_list'] = $info_list;
        }
        if(!empty($detail)){
            return ['status'=>200,'msg'=>$detail];
        }else{
            return ['status'=>500,'msg'=>'未查询到相关数据'];
        }
    }

    /**
     * 添加
     * @param array $detail
     * @return array
     */
    public static function addDetail($detail){
        if(empty($detail)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $check = (new Commons())->checkParameter($detail,[
                'purchase_code|单据编号'=>'require',
                'purchase_time|单据时间'=>'require',
                'info_list|详细信息'=>'require',
            ]);
            if(isset($check['status'])){
                return $check;
            }else{
                $adminId = (new Commons())->getAdminId();
                if(empty($adminId)){
                    return ['status'=>410,'msg'=>'登陆无效，请重新登陆'];
                }
                $status = (new Commons())->checkCode($detail['purchase_code'],26);//验证编号是否合法
                if($status){
                    $examine= (new Commons())->getSet('examine','is_checked');

                    $info_list = (array)json_decode($detail['info_list'], true);

                    $accounts = Db::name('kuyou_accounts')->column('account_name','id');
                    $data = [];
                    $money = 0;
                    foreach ($info_list as $k=>$v){
                        if($v['money']!==''){
                            if($v['money']<=0){
                                return ['status'=>500,'msg'=>'转出金额不能小于0'];
                            }else if(empty($v['settlementtype'])){
                                return ['status'=>500,'msg'=>'请选择结算方式'];
                            }else if(empty($v['accounts_out_id'])){
                                return ['status'=>500,'msg'=>'请选择转出账户'];
                            }else if(empty($v['accounts_enter_id'])){
                                return ['status'=>500,'msg'=>'请选择转入账户'];
                            }else if($v['accounts_out_id']==$v['accounts_enter_id']){
                                return ['status'=>500,'msg'=>'转出账户和转入账户不能为同一账户'];
                            }else{
                                $money += (float)$v['money'];
                                $income_type = Db::name('kuyou_settlementtype')->where(['id'=>$v['settlementtype']])->find();
                                if(empty($income_type)){
                                    return ['status'=>500,'msg'=>'结算方式查询错误'];
                                }elseif (!isset($accounts[$v['accounts_out_id']])){
                                    return ['status'=>500,'msg'=>'转出账户信息有误'];
                                }elseif (!isset($accounts[$v['accounts_enter_id']])){
                                    return ['status'=>500,'msg'=>'转入账户信息有误'];
                                }
                                $data[] = [
                                    'purchase_code'    => $detail['purchase_code'],
                                    'purchase_time'    => strtotime($detail['purchase_time']),
                                    'accounts_out_id'  => $v['accounts_out_id'],
                                    'accounts_enter_id'=> $v['accounts_enter_id'],
                                    'settlementtype_id'=> $v['settlementtype'],
                                    'settlement_number'=> $v['settlement_number']!==''?$v['settlement_number']:null,
                                    'money'            => (float)$v['money'],
                                    'remark'           => $v['remark']!==''?$v['remark']:'',
                                    'createtime'       => time(),
                                    'updatetime'       => time(),
                                ];
                            }
                        }
                    }

                    if($money<=0){
                        return ['status'=>500,'msg'=>'转账金额不能小于0'];
                    }

                    $rowdata = [
                        'purchase_code'     =>  $detail['purchase_code'],
                        'purchase_time'     =>  strtotime($detail['purchase_time']),
                        'money'             =>  $money,
                        'remark'            =>  $detail['remark'],
                        'preparation_id'    =>  $adminId,
                        'operation_id'      =>  $adminId,
                        'createtime'        =>  time(),
                        'updatetime'        =>  time(),
                    ];

                    $model = new AccountsTransfer();
                    $row = $model->where(['purchase_code'=>$detail['purchase_code']])->value('id');
                    if($row){
                        return ['status'=>500,'msg'=>'单据编号已使用，请重新输入单据编号'];
                    }
                    $result = false;
                    Db::startTrans();
                    try {
                        $model->allowField(true)->save($rowdata);
                        $result = $model->id;

                        foreach ($data as $data_k=>$data_v){
                            $data[$data_k]['purchase_id'] = $result;
                        }
                        Db::name('kuyou_accounts_transfer_info')->insertAll($data);
                        Db::commit();
                    } catch (Exception $e) {
                        Db::rollback();
                        return ['status'=>500,'msg'=>$e->getMessage()];
                    }
                    if($result){
                        if($examine!=1){
                            try {
                                $exa = self::examine($detail['purchase_code']);
                            } catch (Exception $e) {
                            }
                            if(!isset($exa['status']) || (isset($exa['status']) && $exa['status']!=1)){
                                try {
                                    self::delDetail($detail['purchase_code']);
                                } catch (Exception $e) {
                                }
                                return ['status'=>500,'msg'=>$exa['msg']];
                            }
                        }
                        return ['status'=>200,'msg'=>$detail['purchase_code']];
                    }else{
                        return ['status'=>500,'msg'=>'添加失败'];
                    }
                }else{
                    return ['status'=>500,'msg'=>'编号规则错误'];
                }
            }
        }
    }

    /**
     * 更新
     * @param array $detail
     * @return array
     */
    public static function editDetail($detail){
        if(empty($detail)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $check = (new Commons())->checkParameter($detail,[
                'old_purchase_code|原单据编号'=>'require',
                'purchase_code|单据编号'=>'require',
                'purchase_time|单据时间'=>'require',
                'info_list|详细信息'=>'require',
            ]);
            if(isset($check['status'])){
                return $check;
            }else{
                $adminId = (new Commons())->getAdminId();
                if(empty($adminId)){
                    return ['status'=>410,'msg'=>'登陆无效，请重新登陆'];
                }
                $status = (new Commons())->checkCode($detail['purchase_code'],26);//验证编号是否合法
                if($status){
                    $model = new AccountsTransfer();
                    $row = $model->where(['purchase_code'=>$detail['old_purchase_code']])->find();
                    if(empty($row)){
                        return ['status'=>500,'msg'=>'修改的单据不存在'];
                    }
                    $old_purchase_code = $detail['old_purchase_code'];
                    $examine= (new Commons())->getSet('examine','is_checked');

                    $info_list = (array)json_decode($detail['info_list'], true);

                    $accounts = Db::name('kuyou_accounts')->column('account_name','id');
                    $data = [];
                    $money = 0;
                    foreach ($info_list as $k=>$v){
                        if($v['money']!==''){
                            if($v['money']<=0){
                                return ['status'=>500,'msg'=>'转出金额不能小于0'];
                            }else if(empty($v['settlementtype'])){
                                return ['status'=>500,'msg'=>'请选择结算方式'];
                            }else if(empty($v['accounts_out_id'])){
                                return ['status'=>500,'msg'=>'请选择转出账户'];
                            }else if(empty($v['accounts_enter_id'])){
                                return ['status'=>500,'msg'=>'请选择转入账户'];
                            }else if($v['accounts_out_id']==$v['accounts_enter_id']){
                                return ['status'=>500,'msg'=>'转出账户和转入账户不能为同一账户'];
                            }else{
                                $money += (float)$v['money'];
                                $income_type = Db::name('kuyou_settlementtype')->where(['id'=>$v['settlementtype']])->find();
                                if(empty($income_type)){
                                    return ['status'=>500,'msg'=>'结算方式查询错误'];
                                }elseif (!isset($accounts[$v['accounts_out_id']])){
                                    return ['status'=>500,'msg'=>'转出账户信息有误'];
                                }elseif (!isset($accounts[$v['accounts_enter_id']])){
                                    return ['status'=>500,'msg'=>'转入账户信息有误'];
                                }
                                $data[] = [
                                    'purchase_code'    => $detail['purchase_code'],
                                    'purchase_time'    => strtotime($detail['purchase_time']),
                                    'accounts_out_id'  => $v['accounts_out_id'],
                                    'accounts_enter_id'=> $v['accounts_enter_id'],
                                    'settlementtype_id'=> $v['settlementtype'],
                                    'settlement_number'=> $v['settlement_number']!==''?$v['settlement_number']:null,
                                    'money'            => (float)$v['money'],
                                    'remark'           => $v['remark']!==''?$v['remark']:'',
                                    'createtime'       => time(),
                                    'updatetime'       => time(),
                                ];
                            }
                        }
                    }

                    if($money<=0){
                        return ['status'=>500,'msg'=>'转账金额不能小于0'];
                    }

                    $rowdata = [
                        'purchase_code'     =>  $detail['purchase_code'],
                        'purchase_time'     =>  strtotime($detail['purchase_time']),
                        'money'             =>  $money,
                        'remark'            =>  $detail['remark'],
                        'preparation_id'    =>  $adminId,
                        'operation_id'      =>  $adminId,
                        'updatetime'        =>  time(),
                    ];

                    if($detail['purchase_code']!=$detail['old_purchase_code']){
                        $purchase_id = $model->where(['purchase_code'=>$detail['purchase_code']])->value('id');
                        if($purchase_id){
                            return ['status'=>500,'msg'=>'单据编号已使用，请重新输入单据编号'];
                        }
                    }

                    if($row){
                        $res = false;
                        Db::startTrans();
                        try {
                            if($examine!=1 && $row['examine_status']==1){
                                self::examinef($detail['old_purchase_code']);
                            }
                            $rowdata['updatetime'] = time();
                            $res = $model->where(['purchase_code'=>$detail['old_purchase_code']])->update($rowdata);

                            Db::name('kuyou_accounts_transfer_info')->where(['purchase_id'=>$row['id']])->delete();
                            foreach ($data as $data_k=>$data_v){
                                $data[$data_k]['purchase_id'] = $row['id'];
                            }
                            Db::name('kuyou_accounts_transfer_info')->insertAll($data);
                            Db::commit();
                        } catch (Exception $e) {
                            Db::rollback();
                            return ['status'=>500,'msg'=>$e->getMessage()];
                        }
                        if($res){
                            if($examine!=1){
                                try {
                                    $exa = self::examine($detail['purchase_code']);
                                } catch (Exception $e) {
                                }
                                if(!isset($exa['status']) || (isset($exa['status']) && $exa['status']!=1)){
                                    return ['status'=>500,'msg'=>$exa['msg']];
                                }
                            }
                            return ['status'=>200,'msg'=>'更新成功'];
                        }else{
                            return ['status'=>500,'msg'=>'更新失败'];
                        }
                    }else{
                        return ['status'=>500,'msg'=>'未查询到可更新数据'];
                    }
                }else{
                    return ['status'=>500,'msg'=>'编号规则错误'];
                }
            }
        }
    }

    /**
     * 删除
     * @param string $purchase_code
     * @return array
     */
    public static function delDetail($purchase_code){
        if(empty($purchase_code)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $purchase_code = explode(',',$purchase_code);
            $model = new AccountsTransfer();
            $examine= (new Commons())->getSet('examine','is_checked');

            $list = $model->where(['purchase_code'=>['in',$purchase_code]])->select();
            $del_id = [];
            foreach ($list as $list_k=>$list_v){
                if($list_v['examine_status']==1 && $examine==1){
                    return ['status'=>500,'msg'=>'不能删除已审核的数据'];
                }else{
                    $del_id[] = $list_v['id'];
                }
            }
            $count = 0;
            $res = false;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
                    if ($v['examine_status']==1 && $examine!=1){
                        try {
                            self::examinef($v['purchase_code']);
                        } catch (Exception $e) {
                        }
                    }
                    $res = $model->where(['purchase_code'=>$v['purchase_code']])->delete();
                    $count += $res;
                }
                Db::name('kuyou_accounts_transfer_info')->where(['purchase_id'=>['in',$del_id]])->delete();
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                return ['status'=>500,'msg'=>$e->getMessage()];
            }
            if($res){
                return ['status'=>200,'msg'=>$count];
            }else{
                return ['status'=>500,'msg'=>'删除失败'];
            }
        }
    }

    /**
     * 审核
     * @param array $purchase_code
     * @return array
     */
    public static function examine($purchase_code = null){
        if(empty($purchase_code)){
            return ['status'=>400,'msg'=>'请选择要操作的数据'];
        }
        $adminId = (new Commons())->getAdminId();
        if(empty($adminId)){
            return ['status'=>410,'msg'=>'登陆无效，请重新登陆'];
        }
        $model = new AccountsTransfer();
        $list = $model->where(['purchase_code'=>['in',$purchase_code],'examine_status'=>'0'])->select();
        if(empty($list)){
            return ['status'=>500,'msg'=>'请选择需要审核的数据'];
        }

        $accounts = Db::name('kuyou_accounts')->select();
        $accounts_list = [];
        foreach ($accounts as $accounts_k=>$accounts_v){
            $accounts_list[$accounts_v['id']] = $accounts_v;
        }

        $list_id = Db::name('kuyou_accounts_transfer')->where(['purchase_code'=>['in',$purchase_code],'examine_status'=>'0'])->column('id');
        $info_list = Db::name('kuyou_accounts_transfer_info')->where(['purchase_id'=>['in',$list_id]])->select();

        $account_list = [];//账户
        $account_log  = [];//账户记录
        foreach ($info_list as $list_k=>$list_v){
            //账户信息
            if(!isset($accounts_list[$list_v['accounts_out_id']])){
                return ['status'=>500,'msg'=>$list_v['purchase_code'].'转出账户有误'];
            }
            $account = $accounts_list[$list_v['accounts_out_id']];
            $status = true;
            foreach ($account_list as $account_list_k=>$account_list_v){
                if($account_list_v['id']==$account['id']){
                    $status = false;
                    $account_list[$account_list_k]['balance'] -= $list_v['money'];
                }
            }
            if($status){
                $account_list[] = [
                    'id'        => $account['id'],
                    'balance'   => $account['balance'] - $list_v['money'],
                ];
            }
            //账户记录
            $account_log[] = [
                'account_coding'    => $account['account_coding'],
                'account_name'      => $account['account_name'],
                'balance'           => $account['balance']-$list_v['money'],
                'expenditure'       => $list_v['money'],
                'pay_time'          => time(),
                'bill_code'         => $list_v['purchase_code'],
                'business_type'     => '11',
                'remark'            => $list_v['remark'],
                'createtime'        => time(),
                'updatetime'        => time(),
            ];

            if(!isset($accounts_list[$list_v['accounts_enter_id']])){
                return ['status'=>500,'msg'=>$list_v['purchase_code'].'转入账户有误'];
            }
            $account = $accounts_list[$list_v['accounts_enter_id']];
            $status = true;
            foreach ($account_list as $account_list_k=>$account_list_v){
                if($account_list_v['id']==$account['id']){
                    $status = false;
                    $account_list[$account_list_k]['balance'] += $list_v['money'];
                }
            }
            if($status){
                $account_list[] = [
                    'id'        => $account['id'],
                    'balance'   => $account['balance'] + $list_v['money'],
                ];
            }
            //账户记录
            $account_log[] = [
                'account_coding'    => $account['account_coding'],
                'account_name'      => $account['account_name'],
                'balance'           => $account['balance']+$list_v['money'],
                'income'            => $list_v['money'],
                'pay_time'          => time(),
                'bill_code'         => $list_v['purchase_code'],
                'business_type'     => '12',
                'remark'            => $list_v['remark'],
                'createtime'        => time(),
                'updatetime'        => time(),
            ];
        }
        $result = false;
        Db::startTrans();
        try {
            if(!empty($account_log)){
                Db::name('kuyou_accounts_log')->insertAll($account_log);
            }
            if(!empty($account_list)){
                (new Accounts())->saveAll($account_list);
            }
            $result = $model->where(['purchase_code'=>['in',$purchase_code],'examine_status'=>'0'])
                ->update(['examine_status'=>1,'examine_id'=>$adminId,'examine_time'=>time(),'operation_id'=>$adminId]);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return ['status'=>500,'msg'=>$e->getMessage()];
        }
        if(isset($result) && $result !== false){
            return ['status'=>1,'msg'=>sizeof($list)];
        }else{
            return ['status'=>500,'msg'=>'审核失败'];
        }
    }

    /**
     * 反审核
     * @param array $purchase_code
     * @return array
     */
    public static function examinef($purchase_code = null){
        if(empty($purchase_code)){
            return ['status'=>400,'msg'=>'请选择要操作的数据'];
        }
        $adminId = (new Commons())->getAdminId();
        if(empty($adminId)){
            return ['status'=>410,'msg'=>'登陆无效，请重新登陆'];
        }

        $model = new AccountsTransfer();
        $list = $model->where(['purchase_code'=>['in',$purchase_code],'examine_status'=>'1'])->select();
        if(empty($list)){
            return ['status'=>500,'msg'=>'请选择需要反审核的数据'];
        }
        $accounts = Db::name('kuyou_accounts')->select();
        $accounts_list = [];
        foreach ($accounts as $accounts_k=>$accounts_v){
            $accounts_list[$accounts_v['id']] = $accounts_v;
        }

        $list_id = Db::name('kuyou_accounts_transfer')->where(['purchase_code'=>['in',$purchase_code],'examine_status'=>'1'])->column('id');
        $info_list = Db::name('kuyou_accounts_transfer_info')->where(['purchase_id'=>['in',$list_id]])->select();

        $account_list = [];//账户
        $account_log  = [];//账户记录
        foreach ($info_list as $list_k=>$list_v){
            //账户信息
            if(!isset($accounts_list[$list_v['accounts_out_id']])){
                return ['status'=>500,'msg'=>$list_v['purchase_code'].'转出账户有误'];
            }
            $account = $accounts_list[$list_v['accounts_out_id']];
            $status = true;
            foreach ($account_list as $account_list_k=>$account_list_v){
                if($account_list_v['id']==$account['id']){
                    $status = false;
                    $account_list[$account_list_k]['balance'] += $list_v['money'];
                }
            }
            if($status){
                $account_list[] = [
                    'id'        => $account['id'],
                    'balance'   => $account['balance'] + $list_v['money'],
                ];
            }
            //账户记录
            $account_log[] = [
                'account_coding'    => $account['account_coding'],
                'account_name'      => $account['account_name'],
                'balance'           => $account['balance']+$list_v['money'],
                'expenditure'       => $list_v['money'],
                'pay_time'          => time(),
                'bill_code'         => $list_v['purchase_code'],
                'business_type'     => '11',
                'remark'            => $list_v['remark'],
                'createtime'        => time(),
                'updatetime'        => time(),
            ];

            if(!isset($accounts_list[$list_v['accounts_enter_id']])){
                return ['status'=>500,'msg'=>$list_v['purchase_code'].'转入账户有误'];
            }
            $account = $accounts_list[$list_v['accounts_enter_id']];
            $status = true;
            foreach ($account_list as $account_list_k=>$account_list_v){
                if($account_list_v['id']==$account['id']){
                    $status = false;
                    $account_list[$account_list_k]['balance'] -= $list_v['money'];
                }
            }
            if($status){
                $account_list[] = [
                    'id'        => $account['id'],
                    'balance'   => $account['balance'] - $list_v['money'],
                ];
            }
            //账户记录
            $account_log[] = [
                'account_coding'    => $account['account_coding'],
                'account_name'      => $account['account_name'],
                'balance'           => $account['balance']-$list_v['money'],
                'income'            => $list_v['money'],
                'pay_time'          => time(),
                'bill_code'         => $list_v['purchase_code'],
                'business_type'     => '12',
                'remark'            => $list_v['remark'],
                'createtime'        => time(),
                'updatetime'        => time(),
            ];
        }
        $result = false;
        Db::startTrans();
        try {
            Db::name('kuyou_accounts_transfer_info')->where(['purchase_id'=>['in',$list_id]])->delete();
            if(!empty($account_list)){
                (new Accounts())->saveAll($account_list);
            }
            $result = $model->where(['purchase_code'=>['in',$purchase_code],'examine_status'=>'1'])
                ->update(['examine_status'=>'0','examine_id'=>null,'examine_time'=>null,'operation_id'=>$adminId]);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return ['status'=>500,'msg'=>$e->getMessage()];
        }
        if(isset($result) && $result !== false){
            return ['status'=>1,'msg'=>sizeof($list)];
        }else{
            return ['status'=>500,'msg'=>'审核失败'];
        }
    }

}
